package com.group.eams.controller;






import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.group.eams.pojo.dto.SupplierQueryDto;
import com.group.eams.pojo.entity.Supplier;
import com.group.eams.pojo.vo.SupplierListVo;
import com.group.eams.service.SupplierService;
import com.group.pojo.dto.BatchDeleteDto;
import com.group.result.Result;
import com.group.result.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;




/**
 * 供应商管理(Supplier)表控制层
 *
 * @author 
 * @since 2024-10-30 12:58:29
 */
@RestController
@RequestMapping("/supplier")
public class SupplierController  {
    /**
     * 服务对象
     */
    @Autowired
    private SupplierService supplierService;

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public Result list( SupplierQueryDto supplierQueryDto) {
        Page<SupplierListVo> page = supplierService.findByQueryDto(supplierQueryDto);

        return Result.buildSuccess(page.getTotal(), page.getRecords());

    }


    @RequestMapping(value = "/findById",method = RequestMethod.GET)
    public Result findById(@RequestParam Long id){
        Supplier supplier = supplierService.getById(id);
        return Result.buildSuccess(supplier);
    }


    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    public Result findAll() {
        return Result.buildSuccess(supplierService.list());
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result add(@RequestBody Supplier supplier){
        supplierService.save(supplier);
        return Result.buildSuccess();
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Result update(@RequestBody Supplier supplier){
        LambdaUpdateWrapper<Supplier> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.eq(Supplier::getId, supplier.getId());
        boolean result = supplierService.update(supplier, queryWrapper);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }


    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public Result delete(@RequestParam Long id){
        LambdaQueryWrapper<Supplier> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Supplier::getId, id);
        boolean result = supplierService.remove(queryWrapper);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }


    @RequestMapping(value = "/batchRemove",method = RequestMethod.POST)
    public Result batchRemove(@RequestBody BatchDeleteDto batchDeleteDto){

        LambdaQueryWrapper<Supplier> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Supplier::getId, batchDeleteDto.getIds());
        boolean result = supplierService.remove(queryWrapper);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }
}

